package com.ruoyi.web.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoWareService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 仓库 controller层
 */
@Controller
@RequestMapping("/storage/ware")
public class StorageController extends BaseController {
    @Autowired
    private RepoWareService repoWareService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private SysProvinceService sysProvinceService;

    @Autowired
    private SysCityService sysCityService;

    @Autowired
    private HttpSession session;

    @RequestMapping("city/{prId}")
    @ResponseBody
    public List<SysCity> getCityById(@PathVariable Long prId) {
        List<SysCity> citys = sysCityService.list(new QueryWrapper<SysCity>().eq("prov_id", prId));
        return citys;
    }

    @RequestMapping()
    public String view(Model model) {
        List<SysProvince> pr = sysProvinceService.list();
        model.addAttribute("prs",pr);
        return "storage/manage/storage";
    }

    @RequiresPermissions("storage:ware:view")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepoWare repoWare1) {
        startPage();
        List<RepoWare> list = repoWareService.getList(repoWare1);
        List<Long> cid = new ArrayList<>();
        for (RepoWare repoWare : list) {
            cid.add(Long.valueOf(repoWare.getSysCreateUserId()));
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCreateName(iSysUserService.selectUserById(cid.get(i)).getUserName());
        }
        return getDataTable(list);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<SysProvince> pr = sysProvinceService.list();
        List<SysUser> usersByDept = iSysUserService.getUsersByDept();
        SysUser user = (SysUser)session.getAttribute("userName");
        model.addAttribute("user",user);
        model.addAttribute("users",usersByDept);
        model.addAttribute("prs",pr);
        return "storage/manage/add";
    }

    @PostMapping("/addRepo")
    @ResponseBody
    public AjaxResult addSave(@Validated RepoWare repoWare)
    {
        return toAjax(repoWareService.save(repoWare));
    }

    @RequestMapping("/details/{id}")
    public String details(Model model,@PathVariable Long id) {
        List<RepoWare> list = repoWareService.getList(new RepoWare());
        RepoWare repoWare = null;
        for (RepoWare re : list) {
            if (Long.valueOf(re.getWareId()) == id) {
                repoWare = re;
            }
        }
        repoWare.setCreateName(iSysUserService.selectUserById(Long.valueOf(repoWare.getSysCreateUserId())).getUserName());
        model.addAttribute("repoWare",repoWare);
        return "storage/manage/detail";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model,@PathVariable Long id) {
        List<RepoWare> list = repoWareService.getList(new RepoWare());
        RepoWare repoWare = null;
        for (RepoWare re : list) {
            if (Long.valueOf(re.getWareId()) == id) {
                repoWare = re;
            }
        }
        model.addAttribute("re",repoWare);

        List<SysUser> usersByDept = iSysUserService.getUsersByDept();
        model.addAttribute("users",usersByDept);
        SysUser user = (SysUser)session.getAttribute("userName");
        model.addAttribute("user",user);

        List<SysProvince> pr = sysProvinceService.list();
        SysCity city = sysCityService.getById(repoWare.getSysCityId());
        SysProvince provId = sysProvinceService.getOne(new QueryWrapper<SysProvince>().eq("prov_id", city.getProvId()));
        List<SysCity> cityList = sysCityService.list(new QueryWrapper<SysCity>().eq("prov_id", provId.getProvId()));
        model.addAttribute("citys",cityList);
        model.addAttribute("cityId",repoWare.getSysCityId());
        model.addAttribute("prs",pr);
        model.addAttribute("prId",provId);
        return "storage/manage/edit";
    }

    @RequestMapping("editRe")
    @ResponseBody
    public AjaxResult update(@Validated RepoWare repoWare) {
        return toAjax(repoWareService.updateById(repoWare));
    }

    @RequestMapping("editStatus")
    @ResponseBody
    public AjaxResult update_status(@Validated RepoWare repoWare) {
        return toAjax(repoWareService.updateById(repoWare));
    }
}
